//
//  HistoryItemWithButtonTableViewCell.m
//  leo
//
//  Created by 张旭 on 15/10/2.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "HistoryItemWithButtonTableViewCell.h"
#import "SyncManager.h"
#import "LeoMsg.h"
#import "DataManager.h"
#import "AppDelegate.h"
@implementation HistoryItemWithButtonTableViewCell

@synthesize cellView;
@synthesize cellLabel;
@synthesize cellCancelButton;
@synthesize cellConfirmButton;
@synthesize navController;
@synthesize message;
@synthesize updateWatchNum;
@synthesize loadingView;
- (void)awakeFromNib {
    // Initialization code
    [self setBackGroudView];
    self.cellCancelButton.enabled=YES;
    self.cellConfirmButton.enabled=YES;
}

- (void)setBackGroudView{
    self.cellView.layer.cornerRadius = 8;
    [self.cellCancelButton.layer setCornerRadius:8];
    [self.cellCancelButton.layer setBorderWidth:1];
    [self.cellCancelButton.layer setBorderColor:[UIColor colorWithRed:200/255.0 green:199/255.0 blue:204/255.0 alpha:1.0].CGColor];
    
    [self.cellConfirmButton.layer setCornerRadius:8];
    [self.cellConfirmButton.layer setBorderWidth:1];
    [self.cellConfirmButton.layer setBorderColor:[UIColor colorWithRed:200/255.0 green:199/255.0 blue:204/255.0 alpha:1.0].CGColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (IBAction)refuese:(id)sender {
    [self chooseAction:NO];
}
- (IBAction)agree:(id)sender {
    [self chooseAction:YES];
}
-(void)chooseAction:(BOOL)chooseValue
{
    loadingView=[LeoMsg showLoadingView];
    [self.superview addSubview:loadingView];
    [loadingView startAnimating];
    self.cellCancelButton.enabled=NO;
    self.cellConfirmButton.enabled=NO;
    SyncManager *syncManager=[[SyncManager alloc] init];
    if(syncManager==NULL)
        return;
    [syncManager bindConfirm:message.deviceUid viceAdminUid:message.uid nickName:message.nickName result:chooseValue randomCode:message.randomCode alias:message.alias completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [loadingView stopAnimating];
        if(result==SUCESS)
        {
            [self updateManagerInfo:chooseValue];
            self.cellCancelButton.enabled=YES;
            self.cellConfirmButton.enabled=YES;
            
        }
        else
        {
            
            [Tooler showHttpMsg:result description:description msg:@"请求失败"];
            self.cellCancelButton.enabled=YES;
            self.cellConfirmButton.enabled=YES;
            
        }
        

    }];
}
-(void)updateManagerInfo:(BOOL)chooseValue
{
    if(!chooseValue)
    {
        [self updateMsg:1];
        return;
    }
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate!=NULL)
        mydelegate.isReceiveBindRelationChange=YES;
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
        return;
    if(message==NULL||message.deviceUid==NULL)
        return;
    Watch *watch=[dataManager getWatch:message.deviceUid];
    if(watch==NULL||watch.groupId==NULL)
        return;
    NSArray *groupWatchs=[dataManager getGroupWatchList:[watch.groupId intValue]];
    if(groupWatchs==NULL||[groupWatchs count]==0)
        return;
    SyncManager *syncManager=[[SyncManager alloc] init];
    if(syncManager==NULL)
        return;
    for(int i=0;i<[groupWatchs count];i++)
    {
        Watch *theWatch=[groupWatchs objectAtIndex:i];
        if(theWatch==NULL)
        {
            updateWatchNum++;
            if(updateWatchNum==[groupWatchs count])
            {
                if(chooseValue)
                {
                    [self updateMsg:2];//2同意
                    updateWatchNum=0;
                }
            }
            continue;
        }
        
        [syncManager getBoundUsers:theWatch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
            updateWatchNum++;
            if(updateWatchNum==[groupWatchs count])
            {
                if(chooseValue)
                {
                    [self updateMsg:2];//2同意
                    updateWatchNum=0;
                }
            }

        }];
    }
}
-(void)updateMsg:(int)chooseVale
{
    [loadingView stopAnimating];
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
    {
        [LeoMsg showMsg:@"程序发生错误，code is 23"];
        return;
    }
    [dataManager updateBindConfirm:self.message.msgId confirmValue:chooseVale];
    [[NSNotificationCenter defaultCenter] postNotificationName:BINDCONFIRMFINISHED object:nil  userInfo:nil];
    [loadingView stopAnimating];
}
@end
